﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
/// <summary>
/// Summary description for Dataconnection
/// </summary>
namespace websitephuongthao.Datasql
{
    public class Dataconnection
    {
        private SqlConnection conn;
        private SqlCommand cmd;
        private string strconnect;
        private string strError;
        public string Error
        {
            get {return strError;}
            set { strError = value; }
        }
        public Dataconnection()
        {
            strconnect = "Data Source=localhost;Initial Catalog=GSPhuongThao;Persist Security Info=True;User ID=sa;Password=123456";
            conn = null;
        }
        public SqlConnection getConnection()
        {
            return new SqlConnection(strconnect);
        }

        public int ExcuteQuery(string StrQuery)
        {
            int kq = 0;
            conn = new SqlConnection(strconnect);
            try
            {
                conn.Open();
                cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = StrQuery;
                cmd.CommandType = CommandType.Text;
                kq = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Error = ex.Message;
            }
            return kq;
        }
        public DataTable GetDataTable_Store(string query)
        {
            try
            {
                conn = getConnection();
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = query;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = conn;
                DataTable table = new DataTable();
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);

                adapter.Fill(table);
                return table;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}